<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                         |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: songaimin@clcw.com.cn  Blog:http://blog.csdn.net/samxx8        |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\Other;

use AuctionCore\System\AuctionException;

/**
 * 公告功能类
 * @author  Eric Lee
 */
class Announcement {

    /**
     * 发布公告
     * @param    int $data
     * @return   Object
     */
    public function add($data = array()) {
        if (empty($data['title'])) {
            AuctionException::make_throw('100058');
        }
        return M('announcement')->add($data);
    }

    /**
     * 修改公告
     * @param    int $data
     * @return   Object
     */
    public function update($data = array()) {
        $ann_id = $data['ann_id'];
        if (empty($ann_id)) {
            AuctionException::make_throw('100056');
        }
        return M('announcement')->where("ann_id='" . $ann_id . "'")->save($data);
    }

    /**
     * 查看公告详细
     * @param    int $ann_id    公告id
     * @return   Object
     */
    public function detail($ann_id) {
        if (empty($ann_id)) {
            AuctionException::make_throw('100056');
        }
        $res = M('announcement')->find($ann_id);
        $res['publisher'] = M('employee')->where("emp_id='" . $res['emp_id'] . "'")->getField('real_name');
        return $res;
    }

    /**
     * 获取公告列表
     * 字段展示：地址，公告标题，日期
     * @param    array $map    条件
     * @param    int $limitRows 每页记录记录数
     * @return   Array
     */
    public function get_list($map = array('status' => array('eq', '0')), $limitRows = '20') {
        //计算记录数
        $total = M('announcement')->where($map)->count();
        $p = new \AuctionCore\Common\AjaxPage($total, $limitRows, "get_list"); //第三个参数是需要调用换页的ajax函数名
        $p->setConfig('first', '首页');
        $p->setConfig('last', '末页');
        $p->setConfig('theme', '  %first%   %upPage%  %linkPage% %prePage% %downPage% %nextPage% %end% %jumpform%');
        $limit_value = $p->firstRow . "," . $p->listRows;
        $res = M('announcement')->where($map)->order("ann_id DESC")->limit($limit_value)->select();
        $list = array();
        foreach ($res as $key => $value) {
            $list[$key] = $value;
            $list[$key]['createtime'] = date('Y-m-d', strtotime($value['createtime']));
        }
        if (I('get.ajax')) {
            echo json_encode(array('list' => $list, 'page' => $p->show()), true);
            exit;
        }
        return array('list' => $list, 'page' => $p->show());
    }

    /*
     * 获取邻居
     * @param int $ann_id 当前公告
     * @param int $symbol 运算符号(> 下一篇 , < 上一篇)
     * @return Array
     * */

    public function get_neighbour($ann_id, $symbol = '>') {
        $map = array();
        $map['ann_id'] = array(($symbol == '>' ? 'gt' : 'lt'), $ann_id);
        $map['status'] = array('eq', '0');
        return M('announcement')->where($map)->order("ann_id " . ($symbol == '>' ? 'ASC' : 'DESC'))->find();
    }

    /**
     * 删除公告
     * @param    int $ann_id    公告ID
     * @return boolen
     */
    public function del($ann_id) {
        if (empty($ann_id)) {
            AuctionException::make_throw('100056');
        }
        $ann_id = M('announcement')->where("ann_id='" . $ann_id . "'")->getField('ann_id');
        if (empty($ann_id)) {
            AuctionException::make_throw('100057');
        }
        return M('announcement')->where($ann_id)->delete();
    }

}

?>